import { useEffect, useRef } from 'react'


function useInterval(callback: Function, delay: number) {
  const savedCallback: any = useRef(null)

  // Remember the latest callback.
  useEffect(() => {
    savedCallback.current = callback
  })

  // Set up the interval.
  useEffect(() => {
    function tick() {
      savedCallback.current()
    }
    if (delay !== null) {
      let id = setInterval(tick, delay)
      return () => clearInterval(id)
    } else {
      return
    }
  }, [delay])
}

export default useInterval
